#!/bin/sh

# Slackware build script for yafray
#
# This script is very fragile and works around the Scons setup, so be
# careful when upgrading to a new version!
#
# Written by Brian Muramatsu (brian@fhobia.org)

PRGNAM=yafray
VERSION=0.0.9
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
# Extracts to "yafray" rather than "yafray-0.0.9" as might be expected.
tar xvzf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM
chown -R root:root . 
chmod -R u+w,go+r-w,a-s .

# SConstruct uses linux-settings.py to parse arguments. In the get_cxxflags,
# it looks for "arch" and "tune" and doesn't seem to look at the regular 
# CFLAGS and CXXFLAGS. These are the only optimization flags I saw in
# linux-settings.py.
if [ "$ARCH" = "i486" ]; then
  SCONFLAGS="arch=i486 tune=i486"
elif [ "$ARCH" = "i586" ]; then
  SCONFLAGS="arch=i586 tune=i586"
elif [ "$ARCH" = "i686" ]; then
  SCONFLAGS="arch=i686 tune=i686"
fi

# linux-settings.py shows that there is only the "prefix" flag to control
# where files will be copied to. Set the prefix to the SBo package directory,
# so that we can work on the package there.
SCONFLAGS="prefix=$PKG/usr $SCONFLAGS"

# Equivalent to the usual "make" step...
scons $SCONFLAGS

# The Yafray wiki says you need to put the flags again or else the information
# about the architecture will be lost, and it will compile it all over again!
#
# http://wiki.yafray.org/bin/view.pl/UserDoc/FaqEng#Compilation
scons $SCONFLAGS install

# Move /usr/etc/gram.yafray to /etc/gram.yafray. Other distributions like
# Debian, Fedora, and Gentoo seem to do the same. At this point, I don't know
# what the impact of moving this is, but it does not seem to impede Yafray's
# function as described in the README.
mkdir $PKG/etc
mv $PKG/usr/etc/gram.yafray $PKG/etc/gram.yafray
rm -rf $PKG/usr/etc

# Yafray's code seems to hardcode /usr/local/lib...so create a symlink from
# /usr/local/lib/yafray to /usr/lib/yafray. This made Blender work properly
# with Yafray immediately after installing this package.
mkdir -p $PKG/usr/local/lib
ln -sf /usr/lib/yafray $PKG/usr/local/lib/yafray

# Copy over documentation. The only difference is there are some technical
# documents under the doc directory that are also copied besides the usual
# suspects.
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS COPYING ChangeLog INSTALL LICENSE NEWS README doc $PKG/usr/doc/$PRGNAM-$VERSION
find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Strip some libraries and binaries
( cd $PKG
   find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true
   find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Build the package
cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
